<?php
class Controller_Admin_Requests extends Controller_Admin_Template
{
    public function before()
    {
            parent::before();
            $this->model = Model::factory('admin_requests');
            $this->itemsPerPage = 6;
    }
        
    public function actionRequests($id = null, $page = 1, $paginator = null)
    {
        $this->addScript('/scripts/autoScroll.js');
        $page = (int)$page - 1;

        if(Request::$isAjax && $id == 'noid'){
            $this->autoRender = false;
            $requests = $this->model->getRequestsWithLimit($page,$this->itemsPerPage);
            foreach($requests  as $request){
                echo '<tr>
                    <td class="border padding10" style="width: 70px;">
                        '.$request["id"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["name"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["last_name"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.(isset($request['residence_type'])?$request['residence_type']:'').'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["email"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["phone"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["address"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.$request["zip_code"].'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.Html::anchor(Request::$controller.'requests/'.$request['id'] .'/','View Details').'
                    </td>

                    <td class="border padding10" style="width: 70px;">
                        '.Html::anchor(Request::$controller . 'remove/' . $request['id'] . '/', 'Delete', array('onclick' => 'javascript:return confirm(CONFIRMATION_REMOVE)')).'
                    </td>
                </tr>';
            }
            exit();
        }

        $this->template->title = 'Manage requests';
        if($id){
            $this->template->content = new View('admin/requestDetails');
            $request = $this->model->getRequestById((int)$id);
            $this->template->content->request = $request;
        }
        else{
            $this->template->content = new View('admin/requests');
            $requests = $this->model->getRequestsWithLimit($page,$this->itemsPerPage);
            $this->template->content->requests = $requests;
        }

    }

    public function actionRemove($id = null){
        $this->model->deleteRequest($id);
        $this->response->redirect(Request::$controller.'requests/');

    }
}
